package booking.gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import booking.misc.User;
public class UserDialog extends JDialog implements ActionListener {
    
    private static final String OK = "ok";
    private static final String CANCEL = "cancel";
    
    User    user = null;
    JTextField nameFld;
    JTextField pwdFld;
    JTextField emailFld;
    JCheckBox adminBox;
    
    
    public UserDialog(JFrame frame, User user) {
        this(frame);
        this.user = user;
        nameFld.setText(user.getUserName());
        pwdFld.setText(user.getUserPassword());
        emailFld.setText(user.getUserEmail());

        adminBox.setSelected(user.getAdmin());

    }
    
    public UserDialog(JFrame frame) {
        setTitle("Add User");
        setModal(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(frame);
        JPanel dataPnl = new JPanel(new GridLayout(5, 5, 5, 5));
        dataPnl.setBorder(new EmptyBorder(5, 5, 5, 5));
        add(dataPnl, BorderLayout.CENTER);
        dataPnl.add(new JLabel("Name:"));
        nameFld = new JTextField(10);
        dataPnl.add(nameFld);
        dataPnl.add(new JLabel("Password:"));
        pwdFld = new JTextField(10);
        dataPnl.add(pwdFld);
        dataPnl.add(new JLabel("Email:"));
        emailFld = new JTextField(10);
        dataPnl.add(emailFld);
     
        
        adminBox = new JCheckBox("Admin?", false);
        dataPnl.add(adminBox);
        
        JPanel bottomPnl = new JPanel();
        add(bottomPnl, BorderLayout.PAGE_END);
        
        JButton btn = new JButton("OK");
        btn.setActionCommand(OK);
        btn.addActionListener(this);
        bottomPnl.add(btn);
        btn = new JButton("Cancel");
        btn.setActionCommand(CANCEL);
        btn.addActionListener(this);
        bottomPnl.add(btn);
        
        pack();
    }
    
    public User getUser() {
        return user;
    }
    public void actionPerformed(ActionEvent event) {
        String ac = event.getActionCommand();
        if (ac.equals(OK)) {
        	
            String userName = nameFld.getText().trim();
            String userPassword = pwdFld.getText().trim();
            String userEmail = emailFld.getText().trim();
            boolean isAdmin = adminBox.isSelected();
            
            if (userName.equals("") || userPassword.equals("") || userEmail.equals(""))	
            	JOptionPane.showMessageDialog(this, "Felaktig inlogging", "FEL",
    					JOptionPane.ERROR_MESSAGE);
            	
            else {
            	int id = (user == null) ? -1 : user.getUserId();
            	user = new User(id, userName, userPassword, userEmail, isAdmin);
            }
            dispose();
        }
        else if (ac.equals(CANCEL)) {
            user = null;
            dispose();
        }
    }
}

